package com.huawei.hms.adapter.b;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.KeyEvent;
import com.huawei.hms.activity.IBridgeActivityDelegate;
import com.huawei.hms.adapter.internal.CommonCode.MapKey;
import com.huawei.hms.support.log.HMSLog;
import java.lang.ref.WeakReference;

public class a implements IBridgeActivityDelegate {
    private WeakReference<Activity> a;
    private String b = "";

    public void onBridgeActivityCreate(Activity activity) {
        this.a = new WeakReference(activity);
        Intent intent = activity.getIntent();
        if (intent == null) {
            c();
            return;
        }
        try {
            intent.getStringExtra("TestIntentProtocol");
            Bundle extras = intent.getExtras();
            this.b = intent.getStringExtra("transaction_id");
            if (extras == null) {
                c();
                return;
            }
            Parcelable parcelable = extras.getParcelable(MapKey.HAS_RESOLUTION);
            if (parcelable == null) {
                c();
            } else if (parcelable instanceof Intent) {
                activity.startActivityForResult((Intent) parcelable, 1001);
            } else if (parcelable instanceof PendingIntent) {
                try {
                    activity.startIntentSenderForResult(((PendingIntent) parcelable).getIntentSender(), 1001, null, 0, 0, 0);
                } catch (SendIntentException e) {
                    c();
                    HMSLog.e("BaseResolutionAdapter", "SendIntentException:exception");
                }
            }
        } catch (Exception e2) {
            HMSLog.e("BaseResolutionAdapter", "intent has some error" + e2.getMessage());
            c();
        }
    }

    public void onBridgeActivityDestroy() {
        HMSLog.i("BaseResolutionAdapter", "onBridgeActivityDestroy");
        this.a = null;
    }

    public boolean onBridgeActivityResult(int i, int i2, Intent intent) {
        if (i != getRequestCode()) {
            return false;
        }
        HMSLog.i("BaseResolutionAdapter", "onBridgeActivityResult");
        if (i2 == -1) {
            com.huawei.hms.adapter.a.a.a().a(intent, this.b);
        } else {
            com.huawei.hms.adapter.a.a.a().a(null, this.b);
        }
        a();
        return true;
    }

    public void onBridgeConfigurationChanged() {
        HMSLog.i("BaseResolutionAdapter", "onBridgeConfigurationChanged");
    }

    public int getRequestCode() {
        return 1001;
    }

    public void onKeyUp(int i, KeyEvent keyEvent) {
        HMSLog.i("BaseResolutionAdapter", "On key up when resolve conn error");
    }

    private void a() {
        Activity b = b();
        if (b != null && !b.isFinishing()) {
            b.finish();
        }
    }

    private Activity b() {
        if (this.a == null) {
            return null;
        }
        return (Activity) this.a.get();
    }

    private void c() {
        com.huawei.hms.adapter.a.a.a().a(null, this.b);
        a();
    }
}
